/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Attachment;
import com.thinkgem.jeesite.modules.sys.service.AttachmentService;

/**
 * 附件信息Controller
 * @author wzl
 * @version 2018-10-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/attachment")
public class AttachmentController extends BaseController {

	@Autowired
	private AttachmentService attachmentService;
	
	@ModelAttribute
	public Attachment get(@RequestParam(required=false) String id) {
		Attachment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = attachmentService.get(id);
		}
		if (entity == null){
			entity = new Attachment();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:attachment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Attachment attachment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Attachment> page = attachmentService.findPage(new Page<Attachment>(request, response), attachment); 
		model.addAttribute("page", page);
		return "modules/sys/attachmentList";
	}

	@RequiresPermissions("sys:attachment:view")
	@RequestMapping(value = "form")
	public String form(Attachment attachment, Model model) {
		model.addAttribute("attachment", attachment);
		return "modules/sys/attachmentForm";
	}

	@RequiresPermissions("sys:attachment:edit")
	@RequestMapping(value = "save")
	public String save(Attachment attachment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attachment)){
			return form(attachment, model);
		}
		attachmentService.save(attachment);
		addMessage(redirectAttributes, "保存附件信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/attachment/?repage";
	}
	
	 
	
	/**
	 * 
	 * @Title: upload 
	 * @author: qjc
	 * @date: 2018年4月12日 上午9:05:04 
	 * @Description: 文件上传
	 * @param: @param file
	 * @param: @param tblId
	 * @param: @param tblName
	 * @param: @param request
	 * @param: @param response
	 * @param: @param status
	 * @param: @param redirectAttributes
	 * @return: String
	 * @throws:
	 */
	@RequestMapping(value = { "upload" })
	public @ResponseBody Attachment upload(@RequestParam("file") MultipartFile file, Attachment pubAttachment, HttpServletRequest request,
	        HttpServletResponse response, Long status) {
		Attachment att = new Attachment();
		try {
			att = attachmentService.upload(file, pubAttachment.getTableId(), pubAttachment.getTableName(), pubAttachment.getType(),request, status);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return att;
	}
	
	/**
	 * 
	 * @Title: download 
	 * @author: qjc
	 * @date: 2018年4月12日 下午3:20:00 
	 * @Description: 下载文件
	 * @param: @param id
	 * @param: @param response
	 * @param: @param request
	 * @param: @throws IOException 
	 * @return: void
	 * @throws:
	 */
	@RequestMapping(value = { "download" })
	public void download(String id, HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		Attachment att = attachmentService.get(id);
		String fileName = att.getFileName();
		try {
			String basepath = String.valueOf(Global.getConfig("filePath") + att.getFilePath());
			File file = new File(basepath);
			FileUtils.downFile(file, request, response, fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Title: searchList 
	 * @author: qjc
	 * @date: 2018年4月14日 下午1:13:14 
	 * @Description: 查询下载列表
	 * @param: @param pubAttachment
	 * @param: @param request
	 * @param: @param response
	 * @param: @return 
	 * @return: List<PubAttachment>
	 * @throws:
	 */
	@RequestMapping(value = { "searchList" })
	public @ResponseBody List<Attachment> searchList(Attachment pubAttachment) {
		List<Attachment> list =attachmentService.findList(pubAttachment);
		return list;
	}
	
	/**
	 * 
	 * @Title: delete 
	 * @author: qjc
	 * @date: 2018年4月13日 上午9:04:16 
	 * @Description: 删除附件
	 * @param: @param pubAttachment
	 * @param: @param redirectAttributes
	 * @param: @return 
	 * @return: String
	 * @throws:
	 */
	@RequestMapping(value = "delete")
	public String delete(String id) {
		Attachment pubAttachment = new Attachment();
		pubAttachment.setId(id);
		attachmentService.delete(pubAttachment);
		return id;
	}

	/**
	 *
	 * @Title: invoicePhotoView
	 * @author: sy
	 * @date: 2019年06月14日 9:36
	 * @Description: 根据表id和表名称查询发票图片url
	 * @param tblId
	 * @param tblName
	 * @return: java.lang.String
	 */
	@ResponseBody
	@RequestMapping(value = "invoicePhotoView")
	public String invoicePhotoView(@RequestParam(required=false) String tblId, @RequestParam(required=false) String tblName,@RequestParam(required=false) String type){
		return attachmentService.invoicePhotoView(tblId,tblName,type);
	}
	
}